<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/3/7
 * Time: 14:47
 */

namespace app\common\model;

/**
 * 收藏模型
 */
class Collection extends ModelBase
{
    public function setCollection($id,$type,$status,$userid=0)
    {
       if(!$id || !$type || !$status || !$userid){
           return false;
       }

       $data['collection'] = $id;
       $data['type'] = $type;
       $data['userid'] = $userid;
       $findRes = $this->getInfo($data);
       $map = $findRes['status'] == -1  ? 1 : -1 ;
       if($findRes){
          $res = $this->setFieldValue($data,'status',$map);
          if($res){
              $code['code'] = 1;
              if($map==1){
                  $code['msg'] = '收藏成功~';
              }elseif($map==-1){
                  $code['msg'] = '已取消收藏~';
              }
          }else{
              $code['code'] = 0;
              if($map==1){
                  $code['msg'] = '收藏失败~';
              }elseif($map==-1){
                  $code['msg'] = '取消收藏失败~';
              }
          }
       }else{
           $res1 = $this->save($data);
           if($res1){
               $code['code'] = 1;
               $code['msg'] = '收藏成功~';
           }else{
               $code['code'] = 0;
               $code['msg'] = '收藏失败~';
           }
       }
       return $code;
    }

    #获取用户收藏
    public function getCollection($user_id)
    {
        $where['userid'] = $user_id;
        $where['status'] = 1;
        return $this->where($where)->select()->toArray();
    }
}
